<!--字典类型-->
<script lang="ts">
export default {
  name: 'dictType'
};
</script>

<script setup lang="ts">
import {
  getDictTypePage,
  getDictTypeForm,
  addDictType,
  updateDictType,
  deleteDictTypes,
  deleteListDictTypes
} from '@/api/dict';

import DictData from '@/views/system/dict/DictData.vue';
import { DictTypePageVO, DictTypeQuery, DictTypeForm } from '@/api/dict/types';
import { statusOptionList } from '@/option'

const queryFormRef = ref(ElForm);
const dataFormRef = ref(ElForm);

const loading = ref(false);
const ids = ref<number[]>([]);
const total = ref(0);

const queryParams = reactive<DictTypeQuery>({
  current: 1,
  pageSize: 10
});

const dictTypeList = ref<DictTypePageVO[]>();

const dialog = reactive<DialogOption>({
  visible: false
});

let formData = reactive<any>({
  status: 1,
  description: "",
});



const rules = reactive({
  label: [{ required: true, message: '请输入字典类型名称', trigger: 'blur' }],
  type: [{ required: true, message: '请输入字典类型编码', trigger: 'blur' }]
});

/**
 * 查询
 */
function handleQuery() {
  loading.value = true;
  getDictTypePage(queryParams)
    .then((res) => {
      dictTypeList.value = res.data.data;
      total.value = res.data.total;
    })
    .finally(() => {
      loading.value = false;
    });
}

/**
 * 重置查询
 */
function resetQuery() {
  queryFormRef.value.resetFields();
  queryParams.current = 1;
  handleQuery();
}

/**
 * 行checkbox change事件
 */
function handleSelectionChange(selection: any) {
  ids.value = selection.map((item: any) => item.id);
}

/**
 * 打开字典类型表单弹窗
 *
 * @param dictVlaue 字典
 */
function openDialog(dictVlaue?: object) {
  dialog.visible = true;
  if (dictVlaue) {
    dialog.title = '修改字典类型';
    // getDictTypeForm(dicTypeId).then(({ data }) => {
    Object.assign(formData, dictVlaue);
    //});
  } else {
    //resetForm();
    dialog.title = '新增字典类型';
  }
}

/**
 * 字典类型表单提交
 */
function handleSubmit() {
  loading.value = false;
  dataFormRef.value.validate((isValid: boolean) => {
    if (isValid) {
      const dictTypeId = formData.id;
      if (dictTypeId) {
        updateDictType(formData)
          .then(() => {
            ElMessage.success('修改成功');
            closeDialog();
            handleQuery();
          })
          .finally(() => (loading.value = false));
      } else {
        console.log(formData)
        addDictType(formData)
          .then(() => {
            ElMessage.success('新增成功');
            closeDialog();
            handleQuery();
          })
          .finally(() => (loading.value = false));
      }
    }
  });
}

/**
 * 关闭弹窗
 */
function closeDialog() {
  dialog.visible = false;
  resetForm();
}

/**
 * 重置表单
 */
function resetForm() {
  dataFormRef.value.resetFields();
  dataFormRef.value.clearValidate();

  // 手动重置
  formData = reactive<DictTypeForm>({
    status: 1,
    description: "",
  });
}

/**
 * 删除字典类型
 */
function handleDelete(id?: number) {
  let idList = ids.value
  id ? idList = [...ids.value, id] : null
  if (idList.length === 0) {
    ElMessage.warning('请勾选删除项');
    return;
  }

  ElMessageBox.confirm('确认删除已选中的数据项?', '警告', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    deleteListDictTypes(idList).then(() => {
      ElMessage.success('删除成功');
      handleQuery();
    });
  });
}

const dictDataDialog = reactive<DialogOption>({
  visible: false
});

interface SelectedDictType {
  id?: number | undefined
}
// 当前选中的字典类型
const selectedDictType = reactive<SelectedDictType>({ id: undefined });

/**
 * 打开字典弹窗
 */
function openDictDialog(row: any) {
  dictDataDialog.visible = true;
  dictDataDialog.title = '【' + row.label + '】字典数据';

  selectedDictType.id = row.id;
}

/**
 * 关闭字典弹窗
 */
function closeDictDialog() {
  dictDataDialog.visible = false;
}

onMounted(() => {
  handleQuery();
});
</script>

<template>
  <div class="app-container">
    <div class="search">
      <el-form ref="queryFormRef" :model="queryParams" :inline="true">
        <el-form-item label="字典类型名称" prop="name">
          <el-input v-model="queryParams.name" placeholder="请输入字典类型名称" clearable @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery()">
            <i-ep-search />搜索
          </el-button>
          <el-button @click="resetQuery()">
            <i-ep-refresh />重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never">
      <template #header>
        <el-button type="success" @click="openDialog()">
          <i-ep-plus />新增
        </el-button>
        <el-button type="danger" :disabled="ids.length === 0" @click="handleDelete()">
          <i-ep-delete />删除
        </el-button>
      </template>
      <el-table highlight-current-row :data="dictTypeList" v-loading="loading" @selection-change="handleSelectionChange"
        border>
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="字典类型名称" prop="label" width="200" align="center" />
        <el-table-column label="字典类型编码" prop="type" width="200" align="center" />
        <!-- <el-table-column label="状态" align="center" width="100">
          <template #default="scope">
            <el-tag v-if="scope.row.status === 1" type="success">启用</el-tag>
            <el-tag v-else type="info">禁用</el-tag>
          </template>
        </el-table-column> -->
        <el-table-column label="描述" prop="description" align="center" />
        <el-table-column label="更新人" prop="lastUpdateBy" width="200" align="center" />
        <el-table-column label="更新时间" prop="lastUpdateTime" width="200" align="center" />
        <el-table-column fixed="right" label="操作" align="center" width="220">
          <template #default="scope">
            <el-button type="primary" link size="small" @click.stop="openDictDialog(scope.row)">
              <i-ep-Collection />字典数据
            </el-button>
            <el-button type="primary" link size="small" @click.stop="openDialog(scope.row)">
              <i-ep-edit />编辑
            </el-button>
            <el-button type="primary" link size="small" @click.stop="handleDelete(scope.row.id)">
              <i-ep-delete />删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-if="total > 0" v-model:total="total" v-model:page="queryParams.current"
        v-model:limit="queryParams.pageSize" @pagination="handleQuery" />
    </el-card>

    <el-dialog :title="dialog.title" v-model="dialog.visible" width="500px" @close="closeDialog">
      <el-form ref="dataFormRef" :model="formData" :rules="rules" label-width="80px">
        <el-form-item label="字典名称" prop="label">
          <el-input v-model="formData.label" placeholder="请输入字典名称" />
        </el-form-item>
        <el-form-item label="字典编码" prop="type">
          <el-input v-model="formData.type" placeholder="请输入字典编码" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="formData.status">
            <el-radio v-for="item in statusOptionList" :key="item.value" :label="item.value">{{ item.label }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="description">
          <el-input v-model="formData.description" type="textarea" placeholder="字典类型备注"
            :autosize="{ minRows: 2, maxRows: 4 }" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="handleSubmit">确 定</el-button>
          <el-button @click="closeDialog">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <!--字典数据弹窗-->
    <el-dialog :title="dictDataDialog.title" v-model="dictDataDialog.visible" width="1000px" @close="closeDictDialog">
      <dict-data v-model:parentId="selectedDictType.id" />
    </el-dialog>
  </div>
</template>
